//
//  CaoZuoDanQueryMainVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/25.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "CaoZuoDanQueryMainVC.h"
#import "CaoZuoDanQueryNavBar.h"
#import "CaoZuoDanMainModel.h"
#import "CaoZuoDanQueryListCell.h"
#import "CaoZuoDanQueryListVC.h"
#import "CZD_TiHuoDetailVC.h"
#import "CZD_DetailOtherVC.h"

@interface CaoZuoDanQueryMainVC ()<ZJScrollPageViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) CaoZuoDanQueryNavBar *navBar;
@property (nonatomic, strong) ZJSegmentStyle *menuStyle;//菜单栏
@property (nonatomic, strong) NSArray<NSString *> *titles;
@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
@property (nonatomic, strong) UIView *scrollPageBGView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <CaoZuoDanMainModel *>*array;

@end

@implementation CaoZuoDanQueryMainVC

-(CaoZuoDanQueryNavBar *)navBar {
    if (!_navBar) {
        _navBar = [[CaoZuoDanQueryNavBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NaviHeight)];
        _navBar.titleStr = @"操作单查询";
        __weak_self;
        _navBar.backBlock = ^{
            [weakself.rt_navigationController popViewControllerAnimated:YES];
        };
        _navBar.changeBlock = ^(NSInteger status) {
            [weakself changeView:status];
        };
    }
    return _navBar;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, ScreenH - BottomHeight - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.hidden = YES;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

-(NSMutableArray<CaoZuoDanMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(ZJScrollPageView *)scrollPageView {
    if (!_scrollPageView) {
        _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, ScreenH - BottomHeight - NaviHeight) segmentStyle:self.menuStyle titles:self.titles parentViewController:self delegate:self];
    }
    return _scrollPageView;
}

-(UIView *)scrollPageBGView {
    if (!_scrollPageBGView) {
        _scrollPageBGView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, 40)];
        _scrollPageBGView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollPageBGView;
}

-(ZJSegmentStyle *)menuStyle {
    if (!_menuStyle) {
        _menuStyle = [ZJSegmentStyle new];
        _menuStyle.autoAdjustTitlesWidth = YES;
        _menuStyle.showLine = YES;//显示下划线
        _menuStyle.scrollTitle = YES;//设置标题栏可以滚动
        _menuStyle.segmentViewBounces = YES;//设置滚动标题栏有弹性
        _menuStyle.scaleTitle = YES;//设置选中标题缩放
        _menuStyle.scrollContentView = YES;//内容可以滑动
        _menuStyle.normalTitleColor = HexColor(0x9C9DB3);
        _menuStyle.selectedTitleColor = Color_ThemeColor;
        _menuStyle.scrollLineColor = Color_ThemeColor;
        _menuStyle.scrollLineWidth = 12;
        _menuStyle.segmentHeight = 40;
        _menuStyle.titleBigScale = 1.1;
        _menuStyle.titleFont = [UIFont systemFontOfSize:14];
    }
    return _menuStyle;
}

-(NSArray<NSString *> *)titles {
    if (!_titles) {
        _titles = @[@"全部",@"新建",@"确认",@"交货出发",@"交货到达",@"干线签收",@"干线出发",@"干线途中",@"干线到达",@"派送签收",@"运单签收",@"回单返回"];
    }
    return _titles;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    page = 1;
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.scrollPageBGView];
    [self.view addSubview:self.scrollPageView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(2) forKey:@"type"];
    [dic setObject:@(page) forKey:@"page"];
    [HttpRequest PostWithApi:LogisticsSupplierCaoZuoDanQueryList params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [CaoZuoDanMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [CaoZuoDanMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)changeView:(NSInteger)status {
    if (status == 1) {
        self.scrollPageBGView.hidden = NO;
        self.scrollPageView.hidden = NO;
        self.tableView.hidden = YES;
    }else {
        self.scrollPageBGView.hidden = YES;
        self.scrollPageView.hidden = YES;
        self.tableView.hidden = NO;
    }
}

- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    CaoZuoDanQueryListVC<ZJScrollPageViewChildVcDelegate> * childVC = (CaoZuoDanQueryListVC *)reuseViewController;
    CaoZuoDanQueryListVC *vc = [CaoZuoDanQueryListVC new];
    if (index == 0) {
        //@"全部"
        vc.status = 0;
    }else if (index == 1) {
        //@"新建"
        vc.status = 1;
    }else if (index == 2) {
        //@"确认"
        vc.status = 2;
    }else if (index == 3) {
        //@"交货出发"
        vc.status = 4;
    }else if (index == 4) {
        //@"交货到达"
        vc.status = 5;
    }else if (index == 5) {
        //@"干线签收"
        vc.status = 6;
    }else if (index == 6) {
        //@"干线出发"
        vc.status = 7;
    }else if (index == 7) {
        //@"干线途中"
        vc.status = 8;
    }else if (index == 8) {
        //@"干线到达"
        vc.status = 9;
    }else if (index == 9) {
        //@"派送签收"
        vc.status = 10;
    }else if (index == 10) {
        //@"运单签收"
        vc.status = 11;
    }else if (index == 11) {
        //@"回单返回"
        vc.status = 12;
    }
    childVC = vc;
    return childVC;
}

//子控制器实现完整生命周期
- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CaoZuoDanQueryListCell *cell = [CaoZuoDanQueryListCell CaoZuoDanQueryListCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CaoZuoDanQueryListCell *cell = [CaoZuoDanQueryListCell CaoZuoDanQueryListCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CaoZuoDanMainModel *model = self.array[indexPath.row];
    if (model.type == CaoZuoDanSupplierTypeTiHuo) {
        CZD_TiHuoDetailVC *vc = [CZD_TiHuoDetailVC new];
        vc.o_id = model.o_id;
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else {
        CZD_DetailOtherVC *vc = [CZD_DetailOtherVC new];
        vc.o_id = model.o_id;
        [self.rt_navigationController pushViewController:vc animated:YES];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
